package com.easepal7506a.project.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by FHB on 2018/4/18.
 */
public class ShowLoadingObserver {

    private static ShowLoadingObserver mInst = null;
    private static Object mLock = new Object();

    private List<onLoadingShow> mOnCloseListerner;

    private ShowLoadingObserver(){
        mOnCloseListerner = new ArrayList<onLoadingShow>();
    }

    public static ShowLoadingObserver getInst(){
        if(mInst == null){
            synchronized(mLock){
                if(mInst == null){
                    mInst = new ShowLoadingObserver();
                }
            }
        }
        return mInst;
    }

    public void  registerObserver(onLoadingShow action){
        if(!mOnCloseListerner.contains(action)){
            mOnCloseListerner.add(action);
        }
    }

    public void removeObserver(onLoadingShow action){
        mOnCloseListerner.remove(action);
    }


    public void noticeShowLoading(){
        for (int i = 0; i < mOnCloseListerner.size(); i++) {
            try {
                mOnCloseListerner.get(i).onShowLoading();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void noticeCancleLoading(){
        for (int i = 0; i < mOnCloseListerner.size(); i++) {
            try {
                mOnCloseListerner.get(i).onCancleLoading();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public interface onLoadingShow {
        void onShowLoading();
        void onCancleLoading();


    }


}
